﻿package com.tudou.home.model.ad.base
{
    import com.tudou.ad.events.*;
    import com.tudou.ad.interfaces.*;
    import com.tudou.events.*;
    import com.tudou.home.consts.*;
    import com.tudou.home.model.ad.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class AdProxyBase extends Proxy
    {
        protected var loaderTime:int = 0;
        protected var delayTimer:Timer;
        protected var _view:IAdItemView;
        protected var loaded:Boolean = false;
        protected var _hasStartView:Boolean = false;
        protected var _tracker:IAdTracker;
        private var _adThrowInfo:IAdThrowInfo;
        protected var loader:TdLoader;
        private var _adItemInfo:IAdItemInfo;
        protected var _hasStart:Boolean = false;

        public function AdProxyBase(param1:String)
        {
            super(param1);
            loaderTime = PlayerConsts.AD_TIMEOUT_TIME * 1000;
            var _loc_2:* = facade.retrieveProxy(AdProxy.NAME) as AdProxy;
            this._tracker = _loc_2.adTracker;
            return;
        }// end function

        protected function startViewCommon() : Boolean
        {
            if (!adItemInfo || _hasStartView)
            {
                return false;
            }
            _hasStartView = true;
            return true;
        }// end function

        protected function frontStuffTimeout() : void
        {
            var _loc_1:Array = [0, 0];
            if (loader)
            {
                _loc_1 = [loader.bytesLoaded, loader.bytesTotal];
            }
            sendTrack(NodeLog.AD_FRONT_STUFF_REQUEST_TIMEOUT, _loc_1);
            return;
        }// end function

        protected function getRemainDt() : int
        {
            return adItemInfo ? (adItemInfo.durationType) : (0);
        }// end function

        private function delayTimerCompleteHlr(event:TimerEvent) : void
        {
            clearDelayTimer();
            delayTimerComplete();
            return;
        }// end function

        protected function ioErrorHlr(event:LoaderEvent) : void
        {
            sendTrack(NodeLog.AD_FRONT_STUFF_REQUEST_ERROR);
            return;
        }// end function

        private function startDelayTimer() : void
        {
            if (delayTimer)
            {
                delayTimer.start();
            }
            return;
        }// end function

        protected function get loadSize() : uint
        {
            return loader ? (loader.bytesLoaded) : (0);
        }// end function

        protected function startLoadFront(param1:String) : void
        {
            if (loader)
            {
                sendTrack(NodeLog.AD_FRONT_STUFF_REQUEST_START);
                _debug("loader.load(v); " + param1);
                loader.load(param1);
            }
            return;
        }// end function

        public function set adThrowInfo(param1:IAdThrowInfo) : void
        {
            _adThrowInfo = param1;
            return;
        }// end function

        protected function sendTrack(param1:String, param2:Array = null) : void
        {
            if (!_tracker)
            {
                return;
            }
            _tracker.adSend(adItemInfo, adThrowInfo, param1, param2);
            return;
        }// end function

        public function startAd(param1:Number = 0) : void
        {
            if (_hasStart)
            {
                return;
            }
            _hasStart = true;
            adStart();
            if (!adItemInfo)
            {
                _debug("!!! mod must check: startAd !adItemInfo, adErrorEnd");
                adErrorEnd();
                return;
            }
            if (param1 > 0)
            {
                clearDelayTimer();
                initDelayTimer(param1);
                startDelayTimer();
            }
            else
            {
                delayTimerComplete();
            }
            return;
        }// end function

        public function startView() : void
        {
            startViewCommon();
            return;
        }// end function

        protected function clearView() : void
        {
            if (!view)
            {
                return;
            }
            removeViewListeners();
            view.clearAd();
            view = null;
            _hasStartView = false;
            return;
        }// end function

        public function get adItemInfo() : IAdItemInfo
        {
            return _adItemInfo;
        }// end function

        public function get view() : IAdItemView
        {
            return _view;
        }// end function

        private function clearDelayTimer() : void
        {
            if (delayTimer)
            {
                delayTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, delayTimerCompleteHlr);
                delayTimer.stop();
            }
            delayTimer = null;
            return;
        }// end function

        private function initDelayTimer(param1:Number) : void
        {
            delayTimer = new Timer(param1, 1);
            delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, delayTimerCompleteHlr);
            return;
        }// end function

        protected function endHlr(event:LoaderEvent) : void
        {
            if (loaded)
            {
                frontStuffComplete();
            }
            else
            {
                frontStuffError();
            }
            return;
        }// end function

        protected function adEnd() : void
        {
            sendTrack(NodeLog.AD_END);
            return;
        }// end function

        protected function viewEventHlr(event:AdItemViewEvent) : void
        {
            return;
        }// end function

        protected function frontStuffComplete() : void
        {
            return;
        }// end function

        protected function adSkip() : void
        {
            sendTrack(NodeLog.AD_SKIP);
            return;
        }// end function

        public function set adItemInfo(param1:IAdItemInfo) : void
        {
            _adItemInfo = param1;
            return;
        }// end function

        public function set view(param1:IAdItemView) : void
        {
            _view = param1;
            return;
        }// end function

        protected function get frontStuff() : DisplayObject
        {
            return loader ? (loader.content) : (null);
        }// end function

        protected function timeoutErrorHlr(event:LoaderEvent) : void
        {
            frontStuffTimeout();
            return;
        }// end function

        public function clearAd() : void
        {
            if (!adItemInfo)
            {
                _debug("!!! mod maybe check: clearAd !adItemInfo");
            }
            clearDelayTimer();
            clearComps();
            clearView();
            adEnd();
            adItemInfo = null;
            adThrowInfo = null;
            _hasStart = false;
            return;
        }// end function

        public function get adThrowInfo() : IAdThrowInfo
        {
            return _adThrowInfo;
        }// end function

        protected function _debug(param1:String) : void
        {
            if (!adItemInfo)
            {
                trace("ad proxy: null " + param1);
                return;
            }
            trace("ad proxy: " + adItemInfo.type + ", " + param1);
            return;
        }// end function

        protected function removeViewListeners() : void
        {
            if (view)
            {
                return;
            }
            var _loc_1:* = view.getEvents();
            var _loc_2:int = 0;
            var _loc_3:* = _loc_1.length;
            while (_loc_2 < _loc_3)
            {
                
                view.removeEventListener(_loc_1[_loc_2], viewEventHlr);
                _loc_2++;
            }
            return;
        }// end function

        protected function delayTimerComplete() : void
        {
            initFront();
            loadFront();
            return;
        }// end function

        protected function clearFront() : void
        {
            if (loader != null)
            {
                loader.removeEventListener(LoaderEvent.COMPLETE, completeHlr);
                loader.removeEventListener(LoaderEvent.END, endHlr);
                loader.removeEventListener(LoaderEvent.IO_ERROR, ioErrorHlr);
                loader.removeEventListener(LoaderEvent.SECURITY_ERROR, ioErrorHlr);
                loader.removeEventListener(LoaderEvent.TIMEOUT_ERROR, timeoutErrorHlr);
                loader.close();
            }
            loader = null;
            return;
        }// end function

        protected function loadFront() : void
        {
            startLoadFront(adItemInfo.url);
            return;
        }// end function

        public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            return;
        }// end function

        protected function get totalSize() : uint
        {
            return loader ? (loader.bytesTotal) : (0);
        }// end function

        public function set tracker(param1:IAdTracker) : void
        {
            _tracker = param1;
            return;
        }// end function

        protected function completeHlr(event:LoaderEvent) : void
        {
            loaded = true;
            sendTrack(NodeLog.AD_FRONT_STUFF_REQUEST_COMPLETE);
            return;
        }// end function

        protected function adErrorEnd() : void
        {
            return;
        }// end function

        protected function clearComps() : void
        {
            clearFront();
            return;
        }// end function

        protected function initFront() : void
        {
            if (loader)
            {
                return;
            }
            loaded = false;
            loader = new TdLoader();
            loader.timeout = loaderTime;
            loader.loaderContext = new LoaderContext(true, null, SecurityDomain.currentDomain);
            loader.addEventListener(LoaderEvent.COMPLETE, completeHlr);
            loader.addEventListener(LoaderEvent.END, endHlr);
            loader.addEventListener(LoaderEvent.IO_ERROR, ioErrorHlr);
            loader.addEventListener(LoaderEvent.SECURITY_ERROR, ioErrorHlr);
            loader.addEventListener(LoaderEvent.TIMEOUT_ERROR, timeoutErrorHlr);
            return;
        }// end function

        protected function frontStuffError() : void
        {
            return;
        }// end function

        protected function createViewListeners() : void
        {
            if (!view)
            {
                return;
            }
            var _loc_1:* = view.getEvents();
            var _loc_2:int = 0;
            var _loc_3:* = _loc_1.length;
            while (_loc_2 < _loc_3)
            {
                
                view.addEventListener(_loc_1[_loc_2], viewEventHlr);
                _loc_2++;
            }
            return;
        }// end function

        protected function adStart() : void
        {
            sendTrack(NodeLog.AD_START);
            return;
        }// end function

    }
}
